package com.hqj.client.controller;

import com.hqj.client.entity.Menu;
import com.hqj.client.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄啟军
 * @version 1.0
 * @date 2020/5/16/016
 */
@Api(tags = "MenuController")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;
    /***
     * Menu分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Menu分页查询",notes = "分页查询Menu方法详情",tags = {"MenuController"})
    @GetMapping(value = "/search/{page}/{size}" )
    public String findPage(@PathVariable  int page,
                               @PathVariable  int size, Model model){
        //调用MenuService实现分页查询Menu
       List<Menu> pageInfo = menuService.findPage(page, size);
       if(!pageInfo.isEmpty()){
           model.addAttribute("findPage",pageInfo);
       }
        return "/page/BlogIndex";
    }
    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Menu根据ID删除",notes = "根据ID删除Menu方法详情",tags = {"MenuController"})
    @DeleteMapping(value = "delete/{id}" )
    public String delete(@PathVariable Long id,Model model){
        //调用MenuService实现根据主键删除
        int i = menuService.delete(id);
        if (i>0){
            model.addAttribute("delete","删除成功");
        }
        return "/page/BlogIndex";
    }

    /***
     * 修改Menu数据
     * @param menu
     * @param id
     * @return
     */
    @ApiOperation(value = "Menu根据ID修改",notes = "根据ID修改Menu方法详情",tags = {"MenuController"})
    @PutMapping(value="update/{id}")
    public String update(@RequestBody @ApiParam(name = "Menu对象",value = "传入JSON数据",required = false) Menu menu,
                         @PathVariable Long id,Model model){
        //设置主键值
        menu.setMenuId(id);
        //调用MenuService实现修改Menu
        int i = menuService.update(menu);
        if (i>0){
            model.addAttribute("update","修改成功");
        }

        return "/page/BlogIndex";
    }

    /***
     * 新增Menu数据
     * @param menu
     * @return
     */
    @ApiOperation(value = "Menu添加",notes = "添加Menu方法详情",tags = {"MenuController"})
    @PostMapping("/insert")
    public String insert(@RequestBody  @ApiParam(name = "Menu对象",value = "传入JSON数据",required = true) Menu menu,
                         Model model){
        //调用MenuService实现添加Menu
        int add = menuService.add(menu);
        if(add>0){
            model.addAttribute("insert","增加成功");
        }

        return "/page/BlogIndex";
    }

    /***
     * 根据ID查询Menu数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Menu根据ID查询",notes = "根据ID查询Menu方法详情",tags = {"MenuController"})
    @GetMapping("findById/{id}")
    public String findById(@PathVariable Long id,Model model){
        //调用MenuService实现根据主键查询Menu
        Menu menu = menuService.findById(id);
        model.addAttribute("findById",menu);
        return "/page/BlogIndex";
    }
    /***
     * 查询Menu全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Menu",notes = "查询所Menu有方法详情",tags = {"MenuController"})
    @GetMapping("/findAll")
    public String findAll(Model model){
        //调用MenuService实现查询所有Menu
        List<Menu> list = menuService.findAll();
        if (!list.isEmpty()){

           model.addAttribute("findAll",list);
        }
        return "/page/BlogIndex";
    }
}
